// midisongloader.h

// 2008-05-01 17:03:47 (Jake): created

#ifndef midisongloader_h
#define midisongloader_h

#include "midiparser.h"
#include "song.h"

struct MidiNote {
	int level;
	int band;

	MidiNote() { }
	MidiNote(int level, int band) : level(level), band(band) { }

	bool operator<(const MidiNote& n) const { 
		return level < n.level || (level == n.level && band < n.band); 
	}
};

class MidiSongLoader : public MidiOutput {
public:
	MidiSongLoader();

	void load(const std::string& file);
	void clear();

	virtual void header(int type, int numTracks, unsigned char b1, unsigned char b0);
	virtual void beginTrack(int track, int size);
	virtual void endTrack();
	virtual void relTime(int t);
	virtual void eventNoteOn(int channel, int note, int velocity);
	virtual void eventNoteOff(int channel, int note, int velocity);
	virtual void tempo(int tempo);

	void addNote(int level, const SongNote& songNote);

	SongNotes& getNotes() { return _notes; }
	SongTempoList& getTempoList() { return _tempoList; }

	int getTicksPerBeat() const { return _ticksPerBeat; }

protected:
	int _type;
	int _numTracks;
	int _track;
	int _time;
	int _division;
	int _fps;
	int _subframe;
	std::map< MidiNote, SongNote > _notesOn;
//	std::map< int, SongNotes > _notes;
	SongNotes _notes;
	SongTempoList _tempoList;
	float _currentTime;
//	int _currentTempo;
	int _ticksPerBeat;

	typedef std::map<unsigned char, MidiNote> MidiNoteMap;
	static MidiNoteMap _midiNoteMap;

	float midiTimeToTime(int midiTimeStart, int midiTimeEnd);
	static void initMidiNoteMap();
	static const MidiNote& getMidiNote(int note);
};

#endif 